/*
 *  This class is part of WorkTimeCalculator Android App
 *  Author: Domenico Pio Novelli
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.dpndeveloper.wtc.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;

import android.util.Log;

import com.dpndeveloper.wtc.dataprovider.WtcInterval;

public class TimeUtil {

	private static DecimalFormat	s_df	= new DecimalFormat("00");

	/**
	 * Return the hours as string for the given time in milliseconds
	 * 
	 * @param millis
	 *            the milliseconds to convert
	 * @return the hours
	 */
	public static String getHours(long millis) {
		long hours = (millis / (1000 * 60 * 60)) % 24;
		long days = millis / (1000 * 60 * 60 * 24);
		return "" + (hours + (days * 24));
	}

	/**
	 * Return the minutes (0-59) as string for the given time in milliseconds
	 * 
	 * @param the
	 *            milliseconds to convert
	 * @return the minutes
	 */
	public static String getMinutes(long millis) {
		long minutes = (millis / (1000 * 60)) % 60;
		return s_df.format(minutes);
	}

	/**
	 * Return the value in milliseconds of worked times in a list of interval.
	 * If an interval doesn't have end time and it's the last one of list, the
	 * end time is the given reference time
	 * 
	 * @param wtcIntervals
	 *            list of interval
	 * @param referenceTime
	 *            the time to use as reference as current time
	 * @return the total worktime
	 */
	public static long getWorkTime(List<WtcInterval> wtcIntervals,
			Calendar referenceTime) {
		long worktime = 0;

		Iterator<WtcInterval> iterator = wtcIntervals.iterator();
		while (iterator.hasNext()) {
			WtcInterval interval = iterator.next();
			long endMills = -1;
			boolean isSameDay = false;
			if (referenceTime != null) {
				endMills = referenceTime.getTimeInMillis();
				isSameDay = interval.getStartTime().get(Calendar.YEAR) == referenceTime
						.get(Calendar.YEAR)
						&& interval.getStartTime().get(Calendar.MONTH) == referenceTime
								.get(Calendar.MONTH)
						&& interval.getStartTime().get(Calendar.DAY_OF_MONTH) == referenceTime
								.get(Calendar.DAY_OF_MONTH);
			}

			// end time is set
			if (interval.getEndTime() != null) {
				endMills = interval.getEndTime().getTimeInMillis();
				worktime = worktime
						+ (endMills - interval.getStartTime().getTimeInMillis());
			} else if (!iterator.hasNext() && isSameDay) {
				// If the interval doesn't have end time and interval is
				// the last of referred day
				worktime = worktime
						+ (endMills - interval.getStartTime().getTimeInMillis());
			} else {
				// if the interval is not the last of list or not the last
				// of referred day
				Log.w("Interval not ended", "Interval not ended");
			}
		}

		return worktime;
	}

	/**
	 * Return the string of worked hours in a list of interval.
	 * 
	 * @param wtcIntervals
	 *            the list of interval
	 * @param referenceTime
	 *            the time to use as reference as current time
	 * @return the total worktime as string
	 */
	public static String getWorkTimeAsString(List<WtcInterval> wtcIntervals,
			Calendar referenceTime) {

		long worktime = getWorkTime(wtcIntervals, referenceTime);
		return TimeUtil.getHours(worktime) + "h "
				+ TimeUtil.getMinutes(worktime) + "m";
	}

	/**
	 * Return the string of worked hours in a list of interval. If an interval
	 * doesn't have end time, the end time is the given reference time
	 * 
	 * @param worktime
	 *            worked time in milliseconds
	 * @return the worktime
	 */
	public static String getWorkTimeAsString(long worktime) {
		return TimeUtil.getHours(worktime) + "h "
				+ TimeUtil.getMinutes(worktime) + "m";
	}

	/**
	 * Return the string of the given calendar formatted as
	 * "dd/MM/yyyy HH:mm:ss"
	 * 
	 * @param calendar
	 *            the calendar to format
	 * @return the formatted string
	 */
	public static String print(Calendar calendar) {
		if (calendar != null) {
			String DATE_FORMAT = "dd/MM/yyyy HH:mm:ss";
			SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
			sdf.setTimeZone(TimeZone.getDefault());
			return sdf.format(calendar.getTime());
		} else
			return "NULL";
	}

	/**
	 * Return the string of the given calendar formatted as "HH:mm"
	 * 
	 * @param calendar
	 *            the calendar to format
	 * @return the formatted string
	 */
	public static String toHoursMinutes(Calendar calendar) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		sdf.setTimeZone(TimeZone.getDefault());
		return sdf.format(calendar.getTime());
	}

}
